package com.hurui.xiaoming.activity.applicationagent;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hurui.xiaoming.R;
import com.hurui.xiaoming.activity.BaseActivity;
import com.hurui.xiaoming.bean.Li_GetAgent_Result;
import com.hurui.xiaoming.bean.Li_Personal_Result;
import com.hurui.xiaoming.myconfig.PamaryKeys;
import com.hurui.xiaoming.utils.WriteReadSharedPrefs;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ApplicationAgentActivity extends BaseActivity {

    TextView title_view ;
    ImageView back_iv ;
    private EditText agentName;
    private EditText agentPhone;
    private EditText agentWeixinNum;
    private EditText agentCompany;
    private EditText agentAddress;
    private EditText agentTjName;
    private EditText agentMessage;
    private Button agentCommit;
    private EditText spinner;
    private Li_Personal_Result.DataBean data;
    private Map<String,String> map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_application_agent);
        data = ((Li_Personal_Result.DataBean) getIntent().getSerializableExtra(PamaryKeys.DATABEAN));
        map=new HashMap<>();
        initView();
        initAction();
    }

    @Override
    public void initView() {
        title_view = (TextView) findViewById(R.id.top_title_tv);
        back_iv = (ImageView) findViewById(R.id.back_iv);
        title_view.setText("申请代理");
        agentName = ((EditText) findViewById(R.id.name_et));
        agentPhone = ((EditText) findViewById(R.id.phone_et));
        agentWeixinNum = ((EditText) findViewById(R.id.weixin_et));
        agentCompany = ((EditText) findViewById(R.id.company_tv));
        agentAddress = ((EditText) findViewById(R.id.address_et));
        agentTjName = ((EditText) findViewById(R.id.recommender_et));
        agentMessage = ((EditText) findViewById(R.id.message_et));
        agentCommit = ((Button) findViewById(R.id.commit_btn));
        spinner = ((EditText) findViewById(R.id.six_spinner));

        if (data.getUi_name()!=null){
            agentName.setText(data.getUi_name().toString());
            setFocuse(agentName);
        }
        if (data.getUi_phone()!=null){
            agentPhone.setText(data.getUi_phone());
            setFocuse(agentPhone);
        }
        if (data.getUi_qqwx()!=null){
            agentWeixinNum.setText(data.getUi_qqwx());
            setFocuse(agentWeixinNum);
        }

        if (data.getUi_sex()!=null){
            //spinner.setSelection(Integer.parseInt(data.getUi_sex().toString()));
            spinner.setText(data.getUi_phone());
            setFocuse(spinner);
        }
    }

    @Override
    public void initAction() {
        back_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onFinish();
            }
        });
        agentCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                commitAgentMessage();
            }
        });
    }

    private void commitAgentMessage() {
        map.put(PamaryKeys.UI_ID, WriteReadSharedPrefs.readUserID(mContext)+"");
        map.put("ui_workname",agentCompany.getText().toString());
        map.put("ui_xzaddress",agentAddress.getText().toString());
        map.put("ay_tjr",agentTjName.getText().toString());
        map.put("ui_xzaddress",agentAddress.getText().toString());
        map.put("ay_mark",agentMessage.getText().toString());
        if (TextUtils.isEmpty(agentCompany.getText().toString())||TextUtils.isEmpty(agentAddress.getText().toString())||TextUtils.isEmpty(agentTjName.getText().toString())
                ||TextUtils.isEmpty(agentAddress.getText().toString())||TextUtils.isEmpty(agentMessage.getText().toString())){
            onToast("请完善信息后再提交");
            return;
        }
        apiSevice.onHttp_applaying(map).enqueue(new Callback<Li_GetAgent_Result>() {
            @Override
            public void onResponse(Call<Li_GetAgent_Result> call, Response<Li_GetAgent_Result> response) {
                if (response.body().getStatus().equals("success")){
                    onToast("申请已提交，审核结果稍后通知");
                    finish();
                }
            }

            @Override
            public void onFailure(Call<Li_GetAgent_Result> call, Throwable t) {
                onToast("服务器连接有误，请检查");
            }
        });

    }

    /**
     * 设置EditView 是否可以编辑
     * @param view
     */
    private void setFocuse(EditText view){
        view.setFocusable(false);
        view.setFocusableInTouchMode(false);

    }
}
